﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Goldlight.Mvvm.Base;
using Goldlight.SampleApplication.ViewModels;
using System.Collections.ObjectModel;
using Goldlight.SampleApplication.Views;
using Goldlight.SampleApplication.ViewModels.Marketing;

namespace Goldlight.SampleApplication.ViewModels
{
    public class MainPageViewModel : MainWindowViewModelBase
    {
        #region Members
        private ReadOnlyCollection<MainWindowViewModelBase> _windows;
        private MainWindowViewModelBase _currentWindow = null;
        private LanguageResources.Language _resources;
        #endregion

        /// <summary>
        /// Initialize a new instance of <see cref="MainPageViewModel"/>.
        /// </summary>
        public MainPageViewModel() : base() { }

        /// <summary>
        /// Get the list of available windows.
        /// </summary>
        public ReadOnlyCollection<MainWindowViewModelBase> Windows
        {
            get { return _windows; }
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            List<MainWindowViewModelBase> list = new List<MainWindowViewModelBase>();
            list.Add(new AccountInformationViewModel());

            _windows = new ReadOnlyCollection<MainWindowViewModelBase>(list);

            Workspaces.Add(new HomePageViewModel());

            _currentWindow = list[0];
            _resources = new Goldlight.SampleApplication.LanguageResources.Language();

            Mediator.Register((object o) => ShowContactManagement(), MediatorMessages.NewContact);
        }

        /// <summary>
        /// Get a reference to the workspaces.
        /// </summary>
        public ObservableCollection<WorkspaceViewModelBase> Workspaces
        {
            get { return WorkspaceManager.Workspaces; }
        }
        public override object Resources
        {
            get
            {
                return _resources;
            }
        }

        /// <summary>
        /// Get the current window to display in the UI.
        /// </summary>
        public MainWindowViewModelBase CurrentWindow
        {
            get { return _currentWindow; }
            set
            {
                if (_currentWindow != value)
                {
                    if (_currentWindow != null)
                    {
                        _currentWindow.IsCurrentWindow = false;
                    }
                    _currentWindow = value;
                    if (_currentWindow != null)
                    {
                        _currentWindow.IsCurrentWindow = true;
                    }
                }
            }
        }

        private void ShowContactManagement()
        {
            ContactManagementViewModel cvm = new ContactManagementViewModel();
            WorkspaceManager.AddWorkspace(cvm);
        }
    }

}
